﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace VIT.Provider
{
    public class Logo : Logo_Base
    {
        #region Variable
        string user = HttpContext.Current.Session["userName"].ToString();
        #endregion

        #region Constructor
        public Logo(int id = -1,
                    string languageId = "",
                    string positionId = "",
                    string link = "",
                    string fileName = "",
                    string fullLogo = "",
                    string type = "",
                    string title = "",
                    string description = "",
                    int orders = 50,
                    bool published = true)
        {
            this.Id = id;
            this.PositionId = positionId;
            this.LanguageId = languageId;
            this.Link = link;
            this.FileName = fileName;
            this.FullLogo = fullLogo;
            this.Type = type;
            this.Title = title;
            this.Description = description;
            this.Orders = orders;
            this.Published = published;
        }
        #endregion

        #region List
        public DataTable GetAll(string SortColumn, string PositionId, bool isTransaction = false)
        {
            string strQuery; 
            if (SortColumn == "") SortColumn = "Orders";
            if (PositionId == "")
                strQuery = "select Logo.Id, Name as Position, Link, FileName, Title, Logo.Description, Orders, Published from Logo, Position where LanguageID=@LanguageID and PositionId=Position.Id and Deleted=0 order by " + SortColumn;
            else
                strQuery = "select Logo.Id, Name as Position, Link, FileName, Title, Logo.Description, Orders, Published from Logo, Position where LanguageID=@LanguageID and PositionId=Position.Id and Deleted=0 and PositionId=@PositionId order by " + SortColumn;
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            SqlParameter PId = new SqlParameter("@PositionId", PositionId);
            return ExecuteDataset(isTransaction, strQuery, LanguageId, PId).Tables[0];
        }

        public bool Delete(int delete, int id, bool isTransaction = false)
        {
            string strQuery = "update Logo set Deleted=@Deleted, Published=@Published, ModifyDate=@Now, ModifyByUser=@User, DeleteByUser=@DeleteByUser where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter D = new SqlParameter("@Deleted", delete);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter DeleteByUser = new SqlParameter("@DeleteByUser", (delete==1)?user:"");
            SqlParameter P = new SqlParameter("@Published", (delete == 0) ? 1 : 0);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, strQuery, Id, D, P, User, DeleteByUser, Now) > 0;            
        }

        public bool Publish(int publish, int id, bool isTransaction = false)
        {
            string strQuery = "update Logo set Published=@Published, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter P = new SqlParameter("@Published", publish);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, strQuery, Id, P, User, Now) > 0;
        }

        public bool Order(string order, int id, bool isTransaction = false)
        {
            string strQuery = "update Logo set Orders=Orders " + order + " 1, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, strQuery, Id, User, Now) > 0;
        }
        #endregion

        #region Edit
        public void GetSingle(bool isTransaction = false)
        {
            if (Exist(isTransaction))
            {
                string strQuery = "select LanguageId, PositionId, Link, FileName, FullLogo, Title, Description, Orders, Published from Logo where Id=@Id";
                SqlParameter Id = new SqlParameter("@Id", this.Id);
                DataTable dt = ExecuteDataset(isTransaction, strQuery, Id).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    this.LanguageId = dt.Rows[0]["LanguageId"].ToString();
                    this.PositionId = dt.Rows[0]["PositionId"].ToString();
                    this.Link = dt.Rows[0]["Link"].ToString();
                    this.FileName = dt.Rows[0]["FileName"].ToString();
                    this.FullLogo = dt.Rows[0]["FullLogo"].ToString();                    
                    this.Title = dt.Rows[0]["Title"].ToString();
                    this.Description = dt.Rows[0]["Description"].ToString();
                    if (!string.IsNullOrEmpty(dt.Rows[0]["Orders"].ToString()))
                        this.Orders = int.Parse(dt.Rows[0]["Orders"].ToString());
                    if (!string.IsNullOrEmpty(dt.Rows[0]["Published"].ToString()))
                        this.Published = (bool)dt.Rows[0]["Published"];
                }
            }
            else this.Id = -1;
        }

        public bool Exist(bool isTransaction = false)
        {
            string strQuery = "select Id from Logo where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            return ExecuteScalar(isTransaction, strQuery, Id) != null;
        }

        public bool Save(bool isTransaction = false)
        {
            TransactionBegin();
            try
            {
                if (Exist(isTransaction))
                {
                    Delete(0, this.Id, isTransaction);
                    Update(isTransaction);
                }
                else this.Id = Insert(isTransaction);

                TransactionCommit();
                return true;
            }
            catch
            {
                TransactionRollback();
                return false;
            }
        }

        public int Insert(bool isTransaction = false)
        {
            string sql = "insert into Logo(LanguageId, PositionId, Link, FileName, FullLogo, Type, Title, Description, Orders, Published, Deleted, CreateDate, ModifyDate, CreateByUser, ModifyByUser)"
                            + " values(@LanguageId, @PositionId, @Link, @FileName, @FullLogo, @Type, @Title, @Description, @Orders, @Published, 0, @Now, @Now, @User, @User)";

            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter PositionId = new SqlParameter("@PositionId", this.PositionId);
            SqlParameter Link = new SqlParameter("@Link", this.Link);
            SqlParameter FileName = new SqlParameter("@FileName", this.FileName);
            SqlParameter FullLogo = new SqlParameter("@FullLogo", this.FullLogo);
            SqlParameter Type = new SqlParameter("@Type", this.Type);
            SqlParameter Title = new SqlParameter("@Title", this.Title);
            SqlParameter Description = new SqlParameter("@Description", this.Description);
            SqlParameter Orders = new SqlParameter("@Orders", this.Orders);
            SqlParameter Published = new SqlParameter("@Published", this.Published);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, sql, LanguageId, PositionId, Link, FileName, FullLogo, Type, Title, Description, Orders, Published, User, Now);
        }

        public int Update(bool isTransaction = false)
        {
            string sqlCat = "update Logo set LanguageId=@LanguageId, PositionId=@PositionId, Link=@Link, FileName=@FileName, FullLogo=@FullLogo, Type=@Type, Title=@Title, Description=@Description, Orders=@Orders, Published=@Published, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter PositionId = new SqlParameter("@PositionId", this.PositionId);
            SqlParameter Link = new SqlParameter("@Link", this.Link);
            SqlParameter FileName = new SqlParameter("@FileName", this.FileName);
            SqlParameter FullLogo = new SqlParameter("@FullLogo", this.FullLogo);
            SqlParameter Type = new SqlParameter("@Type", this.Type);
            SqlParameter Title = new SqlParameter("@Title", this.Title);
            SqlParameter Description = new SqlParameter("@Description", this.Description);
            SqlParameter Orders = new SqlParameter("@Orders", this.Orders);
            SqlParameter Published = new SqlParameter("@Published", this.Published);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, sqlCat, Id, LanguageId, PositionId, Link, FileName, FullLogo, Type, Title, Description, Orders, Published, User, Now);
        }
        #endregion

        #region Trash
        public DataTable GetRecycleBin(string SortColumn = "", bool isTransaction = false)
        {            
            if (SortColumn == "") SortColumn = "Orders";
            string strQuery = "select Logo.Id, Name as Position, Title, Orders, Logo.CreateDate, Logo.ModifyDate, Logo.CreateByUser, Logo.ModifyByUser, Logo.DeleteByUser from Logo, Position where PositionId=Position.Id and Deleted=1 order by " + SortColumn;
            return ExecuteDataset(isTransaction, strQuery).Tables[0];
        }

        public bool Cleared(string id, bool isTransaction = false)
        {
            string strQuery = "delete from Logo where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            return ExecuteNonQuery(isTransaction, strQuery, Id) > 0;
        }
        #endregion
    }
}
